package com.taobao.dingtalk.service.addresslist;

import java.util.List;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiInactiveUserGetRequest;
import com.dingtalk.api.request.OapiInactiveUserV2GetRequest;
import com.dingtalk.api.request.OapiUserCanAccessMicroappRequest;
import com.dingtalk.api.request.OapiUserCountRequest;
import com.dingtalk.api.request.OapiUserGetAdminScopeRequest;
import com.dingtalk.api.request.OapiUserGetbyunionidRequest;
import com.dingtalk.api.request.OapiUserListadminRequest;
import com.dingtalk.api.request.OapiUserListidRequest;
import com.dingtalk.api.request.OapiUserListsimpleRequest;
import com.dingtalk.api.request.OapiV2UserCreateRequest;
import com.dingtalk.api.request.OapiV2UserDeleteRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.request.OapiV2UserListRequest;
import com.dingtalk.api.request.OapiV2UserUpdateRequest;
import com.dingtalk.api.response.OapiInactiveUserGetResponse;
import com.dingtalk.api.response.OapiInactiveUserV2GetResponse;
import com.dingtalk.api.response.OapiUserCanAccessMicroappResponse;
import com.dingtalk.api.response.OapiUserCountResponse;
import com.dingtalk.api.response.OapiUserGetAdminScopeResponse;
import com.dingtalk.api.response.OapiUserGetbyunionidResponse;
import com.dingtalk.api.response.OapiUserListadminResponse;
import com.dingtalk.api.response.OapiUserListidResponse;
import com.dingtalk.api.response.OapiUserListsimpleResponse;
import com.dingtalk.api.response.OapiV2UserCreateResponse;
import com.dingtalk.api.response.OapiV2UserDeleteResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.dingtalk.api.response.OapiV2UserListResponse;
import com.dingtalk.api.response.OapiV2UserUpdateResponse;
import com.taobao.api.ApiException;
import com.taobao.dingtalk.client.DingTalkClientToken;
import com.taobao.dingtalk.service.DingTalkAbstractService;

/**
 * 钉钉用户管理相关<br>
 * 使用时继承本类即可
 * @author nicolas
 *
 */
public class DingTalkUserManageService extends DingTalkAbstractService {

	protected DingTalkUserManageService(DingTalkClientToken dingTalkClientToken) {
		super(dingTalkClientToken);
	}
	/**
	 * 新增用户
	 */
	public String addUser(String userid, String name, String mobile, boolean hideMobile, String telephone,
			String jobNumber, String title, String email, String orgEmail, String workPlace, String remark,
			String deptIdList, String extension, boolean seniorMode, Long hiredDate) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/create");
			OapiV2UserCreateRequest req = new OapiV2UserCreateRequest();
			req.setUserid(userid);
			req.setName(name);
			req.setMobile(mobile);
			req.setHideMobile(hideMobile);
			req.setTelephone(telephone);
			req.setJobNumber(jobNumber);
			req.setTitle(title);
			req.setEmail(email);
			req.setOrgEmail(orgEmail);
			req.setWorkPlace(workPlace);
			req.setRemark(remark);
			req.setDeptIdList(deptIdList);
//			List<OapiV2UserCreateRequest.DeptOrder> list2 = new ArrayList<OapiV2UserCreateRequest.DeptOrder>();
//			OapiV2UserCreateRequest.DeptOrder obj3 = new OapiV2UserCreateRequest.DeptOrder();
//			list2.add(obj3);
//			obj3.setDeptId(2L);
//			obj3.setOrder(1L);
//			req.setDeptOrderList(list2);
//			List<OapiV2UserCreateRequest.DeptTitle> list5 = new ArrayList<OapiV2UserCreateRequest.DeptTitle>();
//			OapiV2UserCreateRequest.DeptTitle obj6 = new OapiV2UserCreateRequest.DeptTitle();
//			list5.add(obj6);
//			obj6.setDeptId(2L);
//			obj6.setTitle("资深产品经理");
//			req.setDeptTitleList(list5);
			req.setExtension(extension);
			req.setSeniorMode(seniorMode);
			req.setHiredDate(hiredDate);
			OapiV2UserCreateResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
			if(rsp != null) {
				return rsp.getResult().getUserid();
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 更新用户
	 */
	public void updateUser(String userid, String name, String mobile, boolean hideMobile, String telephone,
			String jobNumber, String title, String email, String orgEmail, String workPlace, String remark,
			String deptIdList, String extension, boolean seniorMode, Long hiredDate) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/update");
			OapiV2UserUpdateRequest req = new OapiV2UserUpdateRequest();
			req.setUserid(userid);
			req.setName(name);
			req.setMobile(mobile);
			req.setHideMobile(hideMobile);
			req.setTelephone(telephone);
			req.setJobNumber(jobNumber);
			req.setTitle(title);
			req.setEmail(email);
			req.setOrgEmail(orgEmail);
			req.setWorkPlace(workPlace);
			req.setRemark(remark);
			req.setDeptIdList(deptIdList);
//			List<OapiV2UserUpdateRequest.DeptOrder> list2 = new ArrayList<OapiV2UserUpdateRequest.DeptOrder>();
//			DeptOrder obj3 = new DeptOrder();
//			list2.add(obj3);
//			obj3.setDeptId(2L);
//			obj3.setOrder(1L);
//			req.setDeptOrderList(list2);
//			List<OapiV2UserUpdateRequest.DeptTitle> list5 = new ArrayList<OapiV2UserUpdateRequest.DeptTitle>();
//			DeptTitle obj6 = new DeptTitle();
//			list5.add(obj6);
//			obj6.setDeptId(2L);
//			obj6.setTitle("资深产品经理");
			req.setExtension(extension);
			req.setSeniorMode(seniorMode);
			req.setHiredDate(hiredDate);
			req.setLanguage("zh_CN");
			OapiV2UserUpdateResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据用户id删除用户
	 * @param userid
	 */
	public void removeUserByUserid(String userid) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/delete");
			OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest();
			req.setUserid(userid);
			OapiV2UserDeleteResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 根据userid查询用户对象
	 * @param userid
	 */
	public OapiV2UserGetResponse getUserByUserid(String userid) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
			OapiV2UserGetRequest req = new OapiV2UserGetRequest();
			req.setUserid(userid);
			req.setLanguage("zh_CN");
			OapiV2UserGetResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
			return rsp;
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取部门用户基础信息<br>
	 * 调用本接口获取指定部门的用户userid和name。
	 * @param deptId
	 * @param cursor
	 * @param size
	 * @param orderField 排序
	 * <ul>
	 * <li>entry_asc：代表按照进入部门的时间升序。</li>
	 * <li>entry_desc：代表按照进入部门的时间降序。</li>
	 * <li>modify_asc：代表按照部门信息修改时间升序。</li>
	 * <li>modify_desc：代表按照部门信息修改时间降序。</li>
	 * <li>custom：代表用户定义(未定义时按照拼音)排序。</li>
	 * <li>默认值：custom。</li>
	 * </ul>
	 * @param containAccessLimit 是否返回受限人员
	 */
	public void findDeptUserSimpleList(Long deptId, Long cursor, Long size, String orderField, boolean containAccessLimit) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listsimple");
			OapiUserListsimpleRequest req = new OapiUserListsimpleRequest();
			req.setDeptId(deptId);
			req.setCursor(cursor);
			req.setSize(size);
			req.setOrderField(orderField);
			req.setContainAccessLimit(containAccessLimit);
			req.setLanguage("zh_CN");
			OapiUserListsimpleResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取部门用户userid列表<br>
	 * 调用本接口获取指定部门的userid列表<br>
	 * <quote>
	 * 注意 目前暂不支持一次性获取企业下所有员工userid值，如果开发者希望获取企业下所有员工userid值，可以通过以下方法：<br>
	 * 调用获取部门列表接口，通过逐级遍历，获取该企业下所有部门ID。<br>
	 * 调用本接口，分别获取每个部门下的员工userid。</quote>
	 * @param deptId
	 */
	public List<String> findDeptUserIdList(Long deptId) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
			OapiUserListidRequest req = new OapiUserListidRequest();
			req.setDeptId(deptId);
			OapiUserListidResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
			if(rsp != null && rsp.getResult() != null) {
				return rsp.getResult().getUseridList();
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取部门用户详情<br>
	 * 调用本接口获取指定部门中的用户详细信息。
	 * @param deptId
	 * @param cursor
	 * @param size
	 * @param orderField 排序
	 * <ul>
	 * <li>entry_asc：代表按照进入部门的时间升序。</li>
	 * <li>entry_desc：代表按照进入部门的时间降序。</li>
	 * <li>modify_asc：代表按照部门信息修改时间升序。</li>
	 * <li>modify_desc：代表按照部门信息修改时间降序。</li>
	 * <li>custom：代表用户定义(未定义时按照拼音)排序。</li>
	 * <li>默认值：custom。</li>
	 * </ul>
	 * @param containAccessLimit 是否返回受限人员
	 */
	public void findDeptUserList(Long deptId, Long cursor, Long size, String orderField, boolean containAccessLimit) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
			OapiV2UserListRequest req = new OapiV2UserListRequest();
			req.setDeptId(10L);
			req.setCursor(0L);
			req.setSize(10L);
			req.setOrderField("modify_desc");
			req.setContainAccessLimit(false);
			req.setLanguage("zh_CN");
			OapiV2UserListResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取员工人数
	 * @param onlyActive
	 */
	public void getUserCount(Boolean onlyActive) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/count");
			OapiUserCountRequest req = new OapiUserCountRequest();
			req.setOnlyActive(false);
			OapiUserCountResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取未登录钉钉的员工列表
	 * @param isActive
	 * @param deptIds
	 * @param offset
	 * @param size
	 * @param queryDate
	 */
	public void findiNactiveUserList(Boolean isActive, String deptIds, Long offset, Long size, String queryDate) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/inactive/user/v2/get");
			OapiInactiveUserV2GetRequest req = new OapiInactiveUserV2GetRequest();
			req.setIsActive(isActive);
			req.setDeptIds(deptIds);
			req.setOffset(offset);
			req.setSize(size);
			req.setQueryDate(queryDate);
			OapiInactiveUserV2GetResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取管理员列表
	 */
	public OapiUserListadminResponse findUserAdminList() {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listadmin");
			OapiUserListadminRequest req = new OapiUserListadminRequest();
			OapiUserListadminResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
			return rsp;
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取管理员通讯录权限范围
	 * @param userid
	 */
	public void getUserAdminScope(String userid) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/get_admin_scope");
			OapiUserGetAdminScopeRequest req = new OapiUserGetAdminScopeRequest();
			req.setUserid(userid);
			OapiUserGetAdminScopeResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 调用本接口获取管理员的应用管理权限。
	 */
	public void getUserAdminCanAccessMicroapp() {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/can_access_microapp");
			OapiUserCanAccessMicroappRequest req = new OapiUserCanAccessMicroappRequest();
			req.setAppId(1L);
			req.setUserId("1");
			req.setHttpMethod("GET");
			OapiUserCanAccessMicroappResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 通过手机号获取用户信息
	 * @param mobile
	 */
	public String getUserIdByMobile(String mobile) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
			OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
			req.setMobile(mobile);
			OapiV2UserGetbymobileResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
			if(rsp!=null) {
				return rsp.getResult().getUserid();
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 根据unionid查询用户对象
	 * @param unionid
	 */
	public void getUserByUnionid(String unionid) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid");
			OapiUserGetbyunionidRequest req = new OapiUserGetbyunionidRequest();
			req.setUnionid(unionid);
			OapiUserGetbyunionidResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取未登录钉钉的员工列表<br>
	 * 调用本接口查询指定日期内未登录钉钉的企业员工列表。
	 * @param queryDate 查询日期，日期格式为yyyyMMdd。
	 * @param offset 支持分页查询，与size参数同时设置时才生效，此参数代表偏移量，偏移量从0开始。
	 * @param size 支持分页查询，与offset参数同时设置时才生效，此参数代表分页大小，最大100
	 */
	public void findInactiveUserList(String queryDate, Long offset, Long size) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/inactive/user/get");
			OapiInactiveUserGetRequest req = new OapiInactiveUserGetRequest();
			req.setQueryDate(queryDate);
			req.setOffset(offset);
			req.setSize(size);
			OapiInactiveUserGetResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
}
